package com.xbb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbb.entity.Module;
import com.xbb.entity.Role;
import com.xbb.entity.Rolemodules;
import com.xbb.service.IModuleService;
import com.xbb.service.IRolemodulesService;
import com.xbb.service.RoleService;
import com.xbb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  模块控制层
 * </p>
 *
 * @author Mr xie
 * @since 2020-05-19
 */
@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    IModuleService iModuleService;

    /**
     * 显示所有模块---树形
     * @return
     */
    @PostMapping("/getModuleTree")
    public Object getModuleTree() {
        List<Module> list =  iModuleService.getModuleTree();
        return list;
    }

    /**
     * 根据id查询此模块信息
     * @param id
     * @return
     */
    @PostMapping("/getModuleByModId")
    public Object getModuleByModId(@RequestParam Integer id) {
        Module list = iModuleService.getModuleByModId(id);
        if(list!=null){
            return new Result(0,"查询成功",list);
        }
        return new Result(1,"查询失败");
    }


    /**
     * 显示所有模块，并且标识出当前角色拥有的模块--树形
     * @param roleId
     * @return
     */
    @PostMapping("/getModuleTreeByRoleId")
    public Object getModuleTreeByRoleId(@RequestParam Integer roleId) {
        List<Module> list =  iModuleService.getModuleTreeByRoleId(roleId);
        return list;
    }

    /**
     *添加模块（给指定的模块添加子节点）
     * @param module
     * @return
     */
    @PostMapping("/addModule")
    public Object addModule(@Valid Module module){
        Module modueles = iModuleService.getOne(new QueryWrapper<Module>().eq("name", module.getName()));
        //List<Module> modueles = iModuleService.getModueles(module);
        if(modueles!=null){
            return new Result(2,"添加失败，同一个父节点下子节点名称不能相同");
        }
        else{
            boolean save = iModuleService.save(module);
            if(save) {
                return new Result(0,"添加成功");
            }else {
                return new Result(1,"添加失败");
            }

        }

    }

    /**
     *修改模块（修改指定的模块，设置父节点）
     * @param id
     * @param module
     * @return
     */
    @PostMapping("/updateModule")
    public Object updateModule(@RequestParam Integer id, @Valid Module module){
        Module modueles = iModuleService.getOne(new QueryWrapper<Module>().eq("name", module.getName()));
        //List<Module> modueles = iModuleService.getModueles(module);
        if(modueles!=null){
            return new Result(2,"修改失败，同一个父节点下子节点名称不能相同");
        }
        module.setId(id);
        boolean update = iModuleService.updateById(module);
        if(update) {
            return new Result(0,"修改成功");
        }else {
            return new Result(1,"修改失败");
        }
    }

    /**
     *删除指定的模块
     * @param id
     * @return
     */
    @PostMapping("/delModule")
    public Object delModule(@RequestParam Integer id) {
        List<Rolemodules> modContainsRoles = iModuleService.getModContainsRoles(id);
        System.out.println(modContainsRoles.size());
        if(modContainsRoles!=null && modContainsRoles.size()>0){
            return new Result(2,"删除失败，该模块被角色正在拥有,不能删除");
        }else {
            boolean remove = iModuleService.removeById(id);
            if (remove) {
                return new Result(0, "删除成功");
            } else {
                return new Result(1, "删除失败");
            }
        }
    }

    /**
     * 根据指定用户id获取用户拥有的模块集合---树形
     * @param uid
     * @return
     */
    @PostMapping("/getModuleTreeByUserId")
    public Object getModuleTreeByUserId(@RequestParam Integer uid) {
        List<Module> list =  iModuleService.getModuleTreeByUserId(uid);
        return list;
    }

}
